package com.atomjazz.basicgame;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class Ottugi extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(new ottugiView(this));
        //setContentView(R.layout.activity_ottugi);
    }

    class ottugiView extends View {
    	int width, height;
    	int cx, cy;
    	int tw, th;
    	int ang, dir;
    	int an1, an2;
    	
    	Bitmap imgBack, imgToy ;
    	
    	public ottugiView(Context context) {
    		super(context);
    		
    		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    		
    		width = display.getWidth();
    		height = display.getHeight();
    		
    		cx = width / 2;
    		cy = height / 2 + 100;
    		
    		imgBack = BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
    		imgBack = Bitmap.createScaledBitmap(imgBack, width, height, true);
    		
    		imgToy = BitmapFactory.decodeResource(context.getResources(), R.drawable.rabbit03);
    		
    		tw = imgToy.getWidth() / 2 ;
    		th = imgToy.getHeight() ;
    		
    		ang = 0;
    		dir = 0;
    		
    		mHandler.sendEmptyMessageDelayed(0, 10);
    	}

		@Override
		protected void onDraw(Canvas canvas) {
			RotateToy();
			
			canvas.drawBitmap(imgBack, 0, 0, null);
			
			canvas.rotate(ang, cx, cy);
			
			canvas.drawBitmap(imgToy, cx - tw, cy - th, null);
			
			canvas.rotate(-ang, cx, cy);
		}
		
		
    	@Override
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN) {
				an1 = -15;
				an2 = 15;
				if(dir == 0){
					dir = -1;
				}
			}
			return true;
		}

		private void RotateToy() {
    		ang += dir;
    		if(ang <= an1 || ang >= an2) {
    			an1++;
    			an2--;
    			dir = -dir;
    			ang += dir;
    		}
    	}
    	
    	Handler mHandler = new Handler() {
    		public void handleMessage(Message msg) {
    			invalidate();
    			mHandler.sendEmptyMessageDelayed(0, 10);
    		}
    	};
    	
    }
}
